package com.project.controller;

import com.project.bean.BuyBean;
import com.project.bean.GoodsBean;
import com.project.bean.UserBean;
import com.project.service.IBuyDetailService;
import com.project.service.IBuyOrderService;
import com.project.service.IBuyService;
import com.project.service.IGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("buy")
@Api(tags = "采购车控制层")
public class BuyController {
    @Autowired
    private IBuyService buyService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IBuyOrderService buyOrderService;
    @Autowired
    private IBuyDetailService buyDetailService;
    /**
     * 添加采购车
     */
    @ApiOperation("添加购物车")
    @PostMapping("add")
    public String add(@RequestBody String change, GoodsBean goods,Integer buyNum, Integer price,HttpServletRequest request){
        // 判断操作类型是否为"add"，如果是，则调用goodsService的add方法添加商品信息到数据库中
        System.out.println(change+goods+buyNum);
        if (change.equals("add")) {
            goodsService.add(goods);
        }
        // 从HTTP会话中获取当前登录用户的信息
        UserBean loginUser = (UserBean) request.getSession().getAttribute("loginUser");
        // 调用buyService的findByGoodsId方法查询指定用户和商品对应的采购车信息
        BuyBean buy = buyService.findByGoodsId(loginUser.getId(), goods.getId());
        if (buy == null) {
            buyService.add(goods.getId(), price,buyNum, loginUser.getId());
        } else {
            // 否则，调用buyService的updateById方法更新采购车信息的购买数量
            buyService.updateBuyNum(buy.getId(), buy.getNum() + buyNum);
        }
        return "ok";
    }
    @ApiOperation("查询所有购物车")
    @GetMapping("findAll")
    public List<BuyBean> findAll(HttpServletRequest request){
        UserBean loginUser = (UserBean) request.getSession().getAttribute("loginUser");
        return buyService.findAll(loginUser.getId());
    }
    @ApiOperation("通过id删除购物车")
    @DeleteMapping("deleteById")
    public String deleteById(Integer buyCarId){
        buyService.deleteById(buyCarId);
        return "ok";
    }
    @ApiOperation("修改购买价格")
    @PutMapping("updateBuyPrice")
    public String updateBuyPrice(Integer buyId,Integer newBuyPrice){
        buyService.updateBuyPrice(buyId,newBuyPrice);
        return "ok";
    }

    @ApiOperation("修改购买数量")
    @PutMapping("updateBuyNum")
    public String updateBuyNum(Integer buyId,Integer buyNum){
        buyService.updateBuyNum(buyId,buyNum);
        return "ok";
    }

    //入库
    @ApiOperation("入库")
    @PostMapping("putGoods")
    public String putGoods(String supplierName,HttpServletRequest request){
        UserBean loginUser = (UserBean) request.getSession().getAttribute("loginUser");
        //添加订单，并返回该订单的id
            Integer buyOrderId = buyOrderService.add(supplierName, loginUser.getRealName());
        //查询购物车集合
        List<BuyBean> buyCarList = buyService.findAll(loginUser.getId());
        //添加订单明细
        buyDetailService.add(buyOrderId,buyCarList);
        //修改库存商品的最新采购价格和库存数量

        buyCarList.forEach(buyBean -> {
            goodsService.update(buyBean.getFk_goodsId(),buyBean.getPrice(),buyBean.getNum());
        });
        //删除购物车
        buyService.deleteByUserId(loginUser.getId());

        return "ok";
    }



}
